package main;

import java.util.Timer;
import java.util.TimerTask;

import org.apache.log4j.Logger;

public class WatchS {
	private static final Logger LOGGER = Logger.getLogger(WatchS.class);

	private final int TIMERES = 15; // For delaying -- Count in second: 1 = 1 second; 60 = 1 minutes

	public int counter;
	public final int MAXCOUNT = 160; // Elapsed release = (MAXCOUNT * TIMERES / 60) minute(s)

	public WatchS() {
		this.counter = 1;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		LOGGER.info("Start Watch");

		final WatchS watchS = new WatchS();
		final MyExecute myExecute = new MyExecute();
		
		final String st1 = "statement1.sql";
		final String st2 = "statement2.sql";

		// Connect to target database for watching
//		myExecute.doConnectTarget();
//		myExecute.getSQLStr(st1);
//		System.exit(0);

		final Timer timer = new Timer();
		Long delay = (long) (watchS.TIMERES * 1.E3); // 1 minute * Time resolution
		TimerTask task = new TimerTask() {
			@Override
			public void run() {
				myExecute.myExecuteSQL(myExecute.getLocalConnection(), st1, st2); // Execution
				LOGGER.info("Counter = " + watchS.counter);

				synchronized (timer) {
					if(++watchS.counter > watchS.MAXCOUNT) {
						this.cancel();
						timer.cancel(); // Stop timer
					}
				}
			}
		};

		LOGGER.info("Delay time = " + delay);
		timer.schedule(task, 10, delay);
	}

}
